package com.ruoyi.web.controller.system;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.system.SysMenu;
import com.ruoyi.common.core.domain.entity.system.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.miniprogram.LoginRequestBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.miniprogram.service.IMiniprogramUserService;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.web.service.SysLoginService;
import com.ruoyi.web.service.SysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Slf4j
@RestController
public class SysLoginController extends BaseController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private IMiniprogramUserService miniUserService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @PostMapping("/miniprogram/user/login")
    public AjaxResult login(@RequestBody LoginRequestBody requestBody) {
        String code = requestBody.getCode();
        String phone = requestBody.getPhone();
        if (!StringUtils.hasText(code)) {
            return AjaxResult.error(HttpStatus.BAD_REQUEST, "参数code不能为空.");
        }
        if (!StringUtils.hasText(phone)) {
            return AjaxResult.error(HttpStatus.BAD_REQUEST, "手机号码phone不能为空.");
        }
        Map<String, String> resultMap = loginService.loginMiniProgram(code, phone);

        AjaxResult ajax = AjaxResult.success("登录成功");
        ajax.put(Constants.TOKEN, resultMap.get("token"));
//        ajax.put(Constants.USER_ID, resultMap.get("userId")); //此处没必要返回userId

        return ajax;
    }

    /**
     * 小程序端请求获取用户手机号码。
     *
     * @param code
     * @return
     */
    @GetMapping("/miniprogram/getPhoneNumber/{code}")
    public AjaxResult getPhoneNumberOfMiniUser(@PathVariable("code") String code) {
        Map<String, Object> resultMap = loginService.getPhoneNumberOfMiniUser(code);
        return success(resultMap);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
